package com.kai.steering.data.service.ctl;

import com.baomidou.mybatisplus.extension.api.R;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import com.kai.steering.common.web.vo.ReturnCodeExt;
import com.kai.steering.data.service.entity.domainObject.CaretakerDO;
import com.kai.steering.data.service.entity.domainObject.UserDO;
import com.kai.steering.data.service.entity.dto.Login;
import com.kai.steering.data.service.entity.dto.UserQueryDTO;
import com.kai.steering.data.service.service.LoginRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @ Author     ：LiuKai.
 * @ Date       ：Created in 9:24 2022-2-28
 * @ Description：登录注册 前端控制器
 * @ Modified By：
 * @Version:
 */
@Slf4j
@RestController
@RequestMapping("/loginRegister")
@Api(tags = "登录注册 API")
public class LoginRegisterController {
    @Autowired
    private LoginRegisterService loginRegisterService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<String> login(@Valid @RequestBody Login login) {
        /**
         * 参数验证
         */
        //参数
        if (login == null || login.getPassword() == null || login.getUserID() == null || login.getPattern() == null) {//判断参数
            return Result.failed("参数不完整");
        }
        //登录模式
        if (!(login.getPattern() == 1 || login.getPattern() == 0)) {//判断用户属性
            return Result.failed("登录模式错误");
        }
        String result = loginRegisterService.login(login);
        if (result==null){
            return Result.failed("登录失败");
        }



        //跳转 (前端后端都行 给前端)
        return Result.ok(result);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public Result<Boolean> register(@Valid @RequestBody UserQueryDTO userQueryDTO) {
        /**
         * 参数检查
         */
        if (StringUtils.isEmpty(userQueryDTO.getUserId())|| StringUtils.isEmpty(userQueryDTO.getUserName())||
                StringUtils.isEmpty(userQueryDTO.getPassword())) {
            return Result.failed("参数不完整");
        }



        /**
         * 构建对象
         */
        UserDO userDO = new UserDO(userQueryDTO);

        boolean result = loginRegisterService.register(userDO);
        return Result.ok(result);
    }

}
